package com.o2o.utils;

/**
 * package_name:com.o2o.utils
 * Author:徐亚远
 * Date:2019/12/24 21:53
 * 项目名:o2o
 * Desription:
 **/

import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

public class EncryptPropertyPlaceholderConfigurer extends
        PropertyPlaceholderConfigurer {
    //需要加密的字段数组
    private String[] encryptPropNames = { "jdbc.master.username", "jdbc.master.password","jdbc.slave.username",
    "jdbc.slave.password"};

    /*
    * 对关键的属性进行转换
    * */
    @Override
    protected String convertProperty(String propertyName, String propertyValue) {
       if (isEncryptProp(propertyName)) {
            //对已加密的字段进行解密工作
            String decryptValue = DESUtil.getDecryptString(propertyValue);
            return decryptValue;
        } else {
            return propertyValue;
        }
    }

    /*
    *该属性是否已加密
    * @param propertyName
    * @return
    * */
    private boolean isEncryptProp(String propertyName) {
        for (String encryptpropertyName : encryptPropNames) {
            if (encryptpropertyName.equals(propertyName))
                return true;
        }
        return false;
    }
}